package com.zan.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Zan
 * @Create 2024/3/1 14:08
 * @Description : 演示线程池中三种常用分类
 */
public class ThreadPoolDemo1 {
    public static void main(String[] args) {
        /*// 一线程池有n线程（一池5线程）
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5); // 五个窗口

        // 10个顾客请求
        try {
            for (int i = 1; i <= 10; i++) {
                // 执行
                threadPool1.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭线程池 - 释放资源
            threadPool1.shutdown();
        }*/

        /*// 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor(); // 一个窗口
        // 10个顾客请求
        try {
            for (int i = 1; i <= 10; i++) {
                // 执行
                threadPool2.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭线程池 - 释放资源
            threadPool2.shutdown();
        }*/

        // 一池可扩容线程
        ExecutorService threadPool3 = Executors.newCachedThreadPool();

        // 10个顾客请求
        try {
            for (int i = 1; i <= 100; i++) {
                // 执行
                threadPool3.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭线程池 - 释放资源
            threadPool3.shutdown();
        }
    }
}
